package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
        @Autowired
        private SetmealDishService setmealDishService;

        //新增套餐，同时要保持与菜品的关联关系
        @Override
        @Transactional
        public void saveWithDish(SetmealDto setmealDto) {
            //保存套餐基本信息，操作setmeal，执行insert操作
            this.save(setmealDto);

            List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

//            setmealDishes.stream().map((item)->{
//                item.setSetmealId(setmealDto.getId());
//                return item;
//            }).collect(Collectors.toList());
//因为setmealDish中没有将SetmealId保存，所以要保存
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDto.getId());
            }


            //保存套餐和菜品的关联信息，操作setmeal_dish，执行insert操作
            setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 删除套餐，同时删除和套餐关联的菜品的关联数据
     *
     * @param ids
     */
    @Override
    public void removeWithDish(String[] ids) {

    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询套餐基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        //查询套餐菜品信息 回显
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }


    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新setmeal表基本信息
        this.updateById(setmealDto);

        //更新setmeal_dish表信息delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //更新setmeal_dish表信息insert操作
        List<SetmealDish> SetmealDishes = setmealDto.getSetmealDishes();

        SetmealDishes = SetmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(SetmealDishes);
    }



}

